﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using com.lover.astd.common.model;
using com.lover.astd.common.model.building;
using com.lover.astd.common.config;
using System.Windows.Forms;
using System.Collections;
using com.lover.astd.common;
using com.lover.astd.common.logic;
using com.lover.astd.common.model.enumer;
using com.lover.astd.common.logicexe.building;

namespace com.lover.astd.game.ui.server.impl
{
    /// <summary>
    /// 建筑服务
    /// </summary>
    public class BuildingServer : LogicServer
    {
        public BuildingServer(MainForm frm) { _mainForm = frm; ServerName = ConfigStrings.S_Building; ServerReadableName = ConfigStrings.SR_Building; }
        private List<int> getBuildingsToBuild()
        {
            Dictionary<string, string> configs = getConfig(ServerName);
            string confstr = "";
            if (configs.ContainsKey(ConfigStrings.build_ids)) confstr = configs[ConfigStrings.build_ids];
            return generateIds(confstr);
        }
        public override void renderSettings()
        {
            Dictionary<string, string> conf = getConfig(ServerName);
            renderData();
            MainForm frm = _mainForm;
            frm.chk_building_enable.Checked = conf.ContainsKey(ConfigStrings.enabled) && conf[ConfigStrings.enabled].ToLower().Equals("true");
            frm.chk_building_protect.Checked = conf.ContainsKey(ConfigStrings.protect) && conf[ConfigStrings.protect].ToLower().Equals("true");
            if (conf.ContainsKey(ConfigStrings.build_ids) && conf[ConfigStrings.build_ids] != "")
            {
                string build_idstr = conf[ConfigStrings.build_ids];
                renderDataGridToSelected<Building>(frm.dg_buildings, build_idstr);
            }
            refreshData();
        }
        private void renderData()
        {
            if (_mainForm.dg_buildings.DataSource == null)
            {
                BindingSource src = new BindingSource();
                src.DataSource = getUser()._buildings;
                _mainForm.dg_buildings.AutoGenerateColumns = false;
                _mainForm.dg_buildings.DataSource = src;
            }
        }

        private void refreshData()
        {
            _mainForm.dg_buildings.Refresh();
        }
        public override void saveSettings()
        {
            GameConfig conf = getConfig();
            MainForm frm = _mainForm;
            conf.setConfig(ServerName, ConfigStrings.enabled, frm.chk_building_enable.Checked.ToString());
            conf.setConfig(ServerName, ConfigStrings.protect, frm.chk_building_protect.Checked.ToString()); 
            string selectedIds = getDataGridSelectedIds<Building>(frm.dg_buildings);
            conf.setConfig(ServerName, ConfigStrings.build_ids, selectedIds);
        }
        public override void loadDefaultSettings()
        {
            GameConfig conf = getConfig();
            conf.setConfig(ServerName, ConfigStrings.enabled, "true"); // 升级建筑开关
            conf.setConfig(ServerName, ConfigStrings.protect, "true"); // 升级建筑保护开关
            conf.setConfig(ServerName, ConfigStrings.build_ids, ""); // 升级的建筑id字符串, 包含了,分割的建筑id
            renderSettings();
        }
    }
}
